package com.pz.cloud.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author shili.huang
 * @date 2019/5/9
 */
public class FieldUtil {

    private static Logger log = LoggerFactory.getLogger(FieldUtil.class);


    public static Map<String,Field> getAllFieldsToMap(Object object) throws IllegalAccessException {
        Class clazz = object.getClass();
        Map<String,Field> fieldMap = new HashMap<String,Field>(10);
        while (clazz != null){
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields){
                field.setAccessible(true);
                if (fieldMap.get(field.getName()) == null){
                    fieldMap.put(field.getName(),field);
                }
            }
            clazz = clazz.getSuperclass();
        }
        return fieldMap;
    }

    public static Field[] getAllFields(Object object){
        Class clazz = object.getClass();
//        List<Field> fieldList = new ArrayList<>();
        Map<String,Field> fieldMap = new HashMap<>();
        while (clazz != null){
            List<Field> fields = Arrays.asList(clazz.getDeclaredFields());
            for (Field field : fields) {
                if(!fieldMap.containsKey(field.getName())){
                    fieldMap.put(field.getName(),field);
                }

            }
//            fieldList.addAll(new ArrayList<>()));
            clazz = clazz.getSuperclass();
        }
        Field[] fields =  fieldMap.values().toArray(new Field[fieldMap.values().size()]);
//        Field[] fields = new Field[fieldList.size()];
//        fieldList.toArray(fields);
        return fields;
    }

    public static Field[] getAllFields(Class clazz){
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }

    public static Field getFieldByName(Class clazz,String fieldName){
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
       for (Field field : fieldList){
            if (field.getName().equals(fieldName)){
                return field;
            }
       }
        return null;
    }
    public static <T>Object getFieldValueByName(T obj,String fieldName){
        Field field = getFieldByName(obj.getClass(), fieldName);
        if(field!=null){
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                return value;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return null;
    }




}
